Given a multiline string, this function adds css highlighting to it. It highlights all the major Java keywords, comments (single and multiline), and highlights quotes.

Make sure to include css classes for each type of replacement, and make sure that keywords have a space, >,

Also, here is the css snippet that accompanies it:

span.Keyword {
color: blue;
}
span.Quote {
color: red;
}
span.Comment {
color: green;
}

===================================================

<?php
	function getJavaKeys() {
		$keywords = array("abstract","continue","for","new", "null","switch",
						  "assert","default","goto","package","synchronized",
						  "boolean","do","if","private","this",
						  "break","double","implements","protected","throw",
						  "byte","else","import","public","throws",
						  "case","enum","instanceof","return","transient", "true",
						  "catch","extends","int","short","try",
						  "char","false","final","interface","static","void",
						  "class","finally","long","strictfp","volatile",
						  "const","float","native","super","while"
						  );
		return $keywords;
	}	

	 function parseJava($text)
	 {
		$returnText = $text;
		
		// Replace string literals first, so that the inserted text does not interfere
		$index = 0;
		$type = 1;
		while(true) {
			$quote = strpos($returnText, "\"", $index);
			$index = $quote+1;
			if ($index === 1) 
			{
				break;
			}
			
			if ($type === 1) {
				// Add the first <span> tag.
				$firstHalf = substr($returnText, 0, $quote);
				$secondHalf = substr($returnText, $quote);
				$firstHalf = $firstHalf . "<span class='Quote'>";
				
				$returnText = $firstHalf . $secondHalf;
				$index += 20;
				$type = 2;
			}
			elseif ($type === 2) 
			{
				// Add the second <span> tag.
				$firstHalf = substr($returnText, 0, $quote+1);
				$secondHalf = substr($returnText, $quote+1);
				$firstHalf = $firstHalf . "</span>";
				
				$returnText = $firstHalf . $secondHalf;
				$index += 7;
				$type = 1;
			}
		}
		
		// Now replace all of the keywords
		$keywords = getJavaKeys();
		$replacements = array();
		
		// All the replacements are <span> tags and the current word.
		for($i=0; $i<sizeof($keywords); $i++)
		{
			$currentWord = $keywords[$i];
			$replaceText = "<span class='Keyword'>" . $currentWord . "</span>";
			array_push($replacements, $replaceText);
		}
		
		for ($j=0; $j<sizeof($keywords); $j++) 
		{
			// The following statements allow a keyword to be surrounded by spaces, brackets, angle brackets,
			// tabs, and parentheses. This prevents println() from having int be highlighted.
			$returnText = str_replace(" " . $keywords[$j] . " ",  " " . $replacements[$j] . " ", $returnText);
			
			$returnText = str_replace("(" . $keywords[$j] . " ",  "(" . $replacements[$j] . " ", $returnText);
			$returnText = str_replace(" " . $keywords[$j] . "(",  " " . $replacements[$j] . "(", $returnText);
			
			$returnText = str_replace(")" . $keywords[$j] . " ",  ")" . $replacements[$j] . " ", $returnText);
			$returnText = str_replace(" " . $keywords[$j] . ")",  " " . $replacements[$j] . ")", $returnText);
			
			$returnText = str_replace("(" . $keywords[$j] . ")",  "(" . $replacements[$j] . ")", $returnText);
			
			$returnText = str_replace(">" . $keywords[$j] . " ",  ">" . $replacements[$j] . " ", $returnText);
			$returnText = str_replace(" " . $keywords[$j] . "<",  " " . $replacements[$j] . "<", $returnText);
			
			$returnText = str_replace(">" . $keywords[$j] . "<",  ">" . $replacements[$j] . "<", $returnText);
			
			$returnText = str_replace("\t" . $keywords[$j] . " ",  "\t" . $replacements[$j] . " ", $returnText);
			$returnText = str_replace(" " . $keywords[$j] . "\t",  " " . $replacements[$j] . "\t", $returnText);
			
			$returnText = str_replace("\t" . $keywords[$j] . "\t",  "\t" . $replacements[$j] . "\t", $returnText);
		}
		
		// Single line comments!
		$index = 0;
		$type = 1;
		while(true) {
		
		if ($index > strlen($returnText))
		{
			break;
		}
			if ($type === 1) {
				$comment = strpos($returnText, "//", $index);
				$index = $comment+1;
				if ($index === 1 or $index > strlen($returnText)) 
				{
					break;
				}
			
				// Add the first <span> tag.
				$firstHalf = substr($returnText, 0, $comment);
				$secondHalf = substr($returnText, $comment);
				$firstHalf = $firstHalf . "<span class='Comment'>";
				
				$returnText = $firstHalf . $secondHalf;
				$index += 22;
				$type = 2;
				
			}
			else
			{
				$comment = strpos($returnText, "\n", $index);
				$index = $comment+1;
			
				// Add the second <span> tag.
				$firstHalf = substr($returnText, 0, $comment);
				$secondHalf = substr($returnText, $comment);
				$firstHalf = $firstHalf . "</span>";
				
				$returnText = $firstHalf . $secondHalf;
				$index += 7;
				$type = 1;
				
			}
		}
		
		// Replace mulitline comments
		$index = 0;
		$type = 1;
		while(true) {
					
			if ($type === 1) {
				$quote = strpos($returnText, "/*", $index);
				$index = $quote+1;
				if ($index === 1) 
				{
					break;
				}
			
				// Add the first <span> tag.
				$firstHalf = substr($returnText, 0, $quote);
				$secondHalf = substr($returnText, $quote);
				$firstHalf = $firstHalf . "<span class='Comment'>";
				
				$returnText = $firstHalf . $secondHalf;
				$index += 20;
				$type = 2;
			}
			elseif ($type === 2) 
			{
				$quote = strpos($returnText, "*/", $index);
				$index = $quote+1;
				if ($index === 1) 
				{
					break;
				}
				// Add the second <span> tag.
				$firstHalf = substr($returnText, 0, $quote+1);
				$secondHalf = substr($returnText, $quote+1);
				$firstHalf = $firstHalf . "</span>";
				
				$returnText = $firstHalf . $secondHalf;
				$index += 7;
				$type = 1;
			}
		}
		
				
		return $returnText;
		
	 }
?>